
let { log } = console;

// // export default也可以用来输出类。

// // MyClass.js
// export default class A {  }

// // main.js
// import MyClass from 'MyClass';
// let o = new MyClass();


// ES2020提案 引入import()函数，支持动态加载模块。

// import()返回一个 Promise 对象

// 关于http意义，参考 https://developer.mozilla.org/zh-CN/docs/web/api/xmlhttprequest
const getJSON = function (url) {
    const promise = new Promise(function (resolve, reject) {
        const handler = function () {
            if (this.readyState !== 4) {
                return;
            }
            if (this.status === 200) {
                resolve(this.response);
            } else {
                reject(new Error(this.statusText));
            }
        };
        const client = new XMLHttpRequest();
        client.open("GET", url);
        client.onreadystatechange = handler;
        client.responseType = "json";
        client.setRequestHeader("Accept", "application/json");
        client.send();
    });

    return promise;
};
const postJSON = function (url,formData,token=null) {
    const promise = new Promise(function (resolve, reject) {
        const handler = function () {
            if (this.readyState !== 4) {
                return;
            }
            if (this.status === 200) {
                resolve(this.response);
            } else {
                reject(new Error(this.statusText));
            }
        };
        //创建xhr对象 
        const client = new XMLHttpRequest();
        //设置xhr请求的超时时间
        client.timeout = 600000;
        //设置响应返回的数据格式
        client.responseType = "json";
      
        //创建一个 post 请求，采用异步
        client.open('POST', url, true);
        //注册相关事件回调处理函数
        client.onload = function (e) {
            if (this.status == 200 || this.status == 304) {
               // console.log(this.response);
            }
        };
        // client.setRequestHeader("Accept", "application/json");
        client.setRequestHeader("token", token);
        client.onreadystatechange = handler;

        client.ontimeout = function (e) { };
        client.onerror = function (e) { };
        client.upload.onprogress = function (e) { };

        //发送数据
        client.send(formData);

    });

    return promise;
};

export { getJSON ,postJSON};